package com.wkk.nursinghome.controller;

import com.wkk.nursinghome.domain.Bedstaff;
import com.wkk.nursinghome.domain.Employee;
import com.wkk.nursinghome.service.IBedstaffService;
import com.wkk.nursinghome.service.IEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class BedstaffController {
    //注入Service对象
    @Resource
    private IBedstaffService bedstaffService;

    //显示所有信息
    @GetMapping("/bedstaff")
    public String selectAll(Model model){
        //获取所有信息
        List<Bedstaff> bedstaffs=bedstaffService.selectAll();
        //将获取的信息添加到model中
        model.addAttribute("beds",bedstaffs);
        //设置返回的试图名称-->templates/employee.html
        return "bedstaff";
    }

    //删除床位
    @DeleteMapping("/bedstaff/{id}")
    @ResponseBody
    public String deleteById(@PathVariable Integer id){
        //调用方法判断是否删除成功
        boolean idDeleteSuccess=bedstaffService.deleteBedstaffById(id);
        if(idDeleteSuccess){
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 显示修改信息页面
     * @return
     */
    @GetMapping("/bedstaff/{id}")
public String getBedstaffById(@PathVariable("id")Integer id, Model model) {
        Bedstaff bedstaff = bedstaffService.queryById(id);
        model.addAttribute("bed", bedstaff);
        return "updateBedstaff";
    }

    //修改床位信息
    @PutMapping("/bedstaff")
    public String update(Bedstaff bedstaff){
        boolean ismodifysuccess=bedstaffService.updateBedstaff(bedstaff);
        if(ismodifysuccess) {
            return "redirect:/bedstaff";
        }else{
            return "first";
        }
    }

    //显示新增床位信息页面
    @GetMapping("/addBed")
    public String addBedf(){
        return "/addBedstaff";
    }
    //增加床位信息
    @PutMapping("/addB")
    public String add(Bedstaff bedstaff){
        bedstaffService.addBedstaff(bedstaff);
        return "redirect:/bedstaff";
    }

    //根据条件查询
    @GetMapping("/searchB")
    public String searchByCondition(Model model,Bedstaff bedstaff){
        List<Bedstaff> bedstaffs= bedstaffService.selectByCondition(bedstaff);
        model.addAttribute("beds",bedstaffs);
        return "/bedstaff";
    }

}
